package com.hippo.core.service;

import com.alibaba.fastjson.JSONObject;
import com.hippo.core.dao.ElementDao;
import com.hippo.core.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: hippo
 */
@Service
public class ElementService {

    @Autowired
    private ElementDao elementDao;

    public JSONObject getElementList(JSONObject obj) {
        CommonUtil.fillPageParam(obj);
        int count = elementDao.countElement(obj);
        List<JSONObject> list = elementDao.getElementList(obj);
        return CommonUtil.successPage(obj, list, count);
    }

    public JSONObject getAllElementList(JSONObject obj) {
        CommonUtil.fillPageParam(obj);
        List<JSONObject> list = elementDao.getElementList(obj);
        return CommonUtil.successJson(list);
    }

    public JSONObject addElement(JSONObject obj) {
        Integer countByName = elementDao.getCountByName(obj);
        System.out.println(countByName);
        if(countByName == 0){
            return CommonUtil.response(elementDao.addElement(obj));
        }else{
            return CommonUtil.successJson("元素已存在");
        }
    }

    public JSONObject updateElement(JSONObject obj) {
        return CommonUtil.response(elementDao.updateElement(obj));
    }

    public JSONObject deleteElement(String id) {
        return CommonUtil.response(elementDao.deleteElement(id));
    }

}

